import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm

# Data preparation
data = [
    (500, 2000, 4, 23200),
    (500, 5000, 10, 25150),
    (500, 10000, 20, 28400),
    (1000, 2000, 2, 45650),
    (1000, 5000, 5, 47600),
    (1000, 10000, 10, 50850),
    (2000, 2000, 1, 90550),
    (2000, 5000, 2.5, 92500),
    (2000, 10000, 5, 95750),
]

clap_values = sorted(set([entry[0] for entry in data]))
plac_values = sorted(set([entry[1] for entry in data]))

# Prepare the grid for plotting
xpos, ypos = np.meshgrid(clap_values, plac_values)
xpos, ypos = xpos.flatten(), ypos.flatten()
zpos = np.zeros_like(xpos)

# The z coordinate is the TAC values, and color will be based on the ratio
dz = [entry[3] for entry in data]
ratios = [entry[2] for entry in data]  # Assuming ratio is the third element

# Prepare figure and 3D axis
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot with color mapping and increased width/depth for clarity
dx = dy = 0.7 * 200
alpha = 0.7
colors = cm.viridis(ratios)  # Using viridis colormap for ratios
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=colors, alpha=alpha, zsort='average')

# Set labels and title
ax.set_xlabel('CLAP')
ax.set_ylabel('PLAC')
ax.set_zlabel('Average TAC of B.S. [$]')
ax.set_title('3D Bar Chart of Average TAC by CLAP and PLAC')

# Adjust the viewing angle
ax.view_init(elev=45, azim=-35)

# Show plot
plt.show()
